package com.xxx.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.mall.config.MinioTemplate;
import com.xxx.mall.context.AuthUserContext;
import com.xxx.mall.converter.AttachFileConverter;
import com.xxx.mall.dto.AttachFileDTO;
import com.xxx.mall.dto.PageQuery;
import com.xxx.mall.exception.MallException;
import com.xxx.mall.mapper.AttachFileMapper;
import com.xxx.mall.model.AttachFile;
import com.xxx.mall.service.AttachFileService;
import com.xxx.mall.vo.AttachFileVO;
import com.xxx.mall.vo.PageVO;
import org.springframework.aop.framework.AopContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 上传文件记录表 服务实现类
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-11-20
 */
@Service
public class AttachFileServiceImpl extends ServiceImpl<AttachFileMapper, AttachFile> implements AttachFileService {

    @Resource
    AttachFileConverter attachFileConverter;

    @Resource
    MinioTemplate minioTemplate;

    @Override
    public PageVO<AttachFileVO> pageList(PageQuery pageQuery, String fileName, Long fileGroupId) {
        return PageVO.of(baseMapper.pageList(PageDTO.of(pageQuery.getPage(), pageQuery.getSize()), fileName, fileGroupId, AuthUserContext.get().getTenantId()));
    }

    @Override
    public void saveInfo(List<AttachFileDTO> list) {
        List<AttachFile> attachFile = attachFileConverter.toAttachFile(list);
        attachFile.forEach(file -> file.setShopId(AuthUserContext.get().getTenantId()));
        AttachFileService proxy = (AttachFileService) AopContext.currentProxy();
        proxy.saveBatch(attachFile);
    }

    @Override
    public void updateNameOrGroupById(AttachFileDTO dto) {
        if (dto.getFileName() == null && dto.getAttachFileGroupId() == null) {
            return;
        }
        baseMapper.updateNameOrGroupById(dto.getFileId(), dto.getFileName(), dto.getAttachFileGroupId());
    }

    @Override
    public void deleteById(Long id) {
        baseMapper.removeById(id);
        deleteFileByIds(Collections.singletonList(id));
    }

    @Override
    public void deleteBatchByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        baseMapper.removeBatchByIds(ids);
        deleteFileByIds(ids);
    }

    @Async
    public void deleteFileByIds(List<Long> ids) {
        List<AttachFile> attachFiles = baseMapper.selectBatchIds(ids);
        List<String> list = attachFiles.stream().map(item -> {
            String filePath = item.getFilePath();
            if (StrUtil.isNotBlank(filePath)) {
                filePath = filePath.substring(1);
                try {
                    minioTemplate.removeObject(filePath);
                } catch (Exception e) {
                    throw new MallException("删除文件失败" + e.getMessage());
                }
            }
            return filePath;
        }).collect(Collectors.toList());
        minioTemplate.removeObjects(list);
    }
}
